package org.zezi.loader.ui.adapter;

import org.zezi.loader.R;
import org.zezi.loader.provider.ShopListContract.Items;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ItemsCursorAdapter extends CursorAdapter {

	private final LayoutInflater mInflater;
	
	private int mIndexName; 
	private int mIndexCount; 
	
	public ItemsCursorAdapter(Context context, Cursor c, int i) {
		super(context, c, i);
		
		mInflater = LayoutInflater.from(context);
		
	}

	@Override
	public View newView(Context context, Cursor c, ViewGroup parent) {
		final View view = mInflater.inflate(R.layout.row_item, null);
        final ViewHolder holder = new ViewHolder(view);
        view.setTag(holder);

        return view;
	}
	
	@Override
	public void bindView(View view, Context arg1, Cursor c) {
		
		ViewHolder viewHolder = (ViewHolder) view.getTag();
		
		mIndexName = c.getColumnIndexOrThrow(Items.COLUMN_NAME_NAME);
		mIndexCount = c.getColumnIndexOrThrow(Items.COLUMN_NAME_COUNT);
		
        final String name = c.getString(mIndexName);
        final String count = c.getString(mIndexCount);
        
        viewHolder.name.setText(name);
        viewHolder.count.setText(count);
	}
	
	public class ViewHolder {
		public TextView name;
		public TextView count;

		public ViewHolder(View base) {
			name = (TextView) base.findViewById(R.id.row_item_name);
			count = (TextView) base.findViewById(R.id.row_item_count);
		}
	}

}
